#define DIRECTINPUT_VERSION 0x0800

#include "GroundInputComponent.h"
#include <dinput.h>

BYTE GroundInputComponent::bindings[11] = {DIK_UP, DIK_DOWN, DIK_RIGHT, DIK_LEFT, DIK_O};
IEngineComponentRegistry groundInputComponentRegistery("GroundInputComponent", GroundInputComponent::Deserialize);

GroundInputComponent::GroundInputComponent()
{
	this->groundId = -1;
}

GroundInputComponent::GroundInputComponent(long long int groundId)
{
	this->groundId = groundId;
}

GroundInputComponent::~GroundInputComponent(void)
{
}

void GroundInputComponent::setGroundId(long long int groundId)
{
	this->groundId = groundId;
}

void GroundInputComponent::getHoldBindings(BYTE** bindings, DWORD* nBindings)
{
	*bindings = this->bindings;
	*nBindings = 5;
}

void GroundInputComponent::keyHold(BYTE key, LONGLONG timeElapsed)
{
	switch (key)
	{
		case DIK_UP:
			ground->move(D3DXVECTOR3(0,0,(float)(.0001 * timeElapsed)));
			break;
		case DIK_DOWN:
			ground->move(D3DXVECTOR3(0,0,(float)(-.0001 * timeElapsed)));
			break;
		case DIK_RIGHT:
			ground->move(D3DXVECTOR3((float)(.0001 * timeElapsed),0,0));
			break;
		case DIK_LEFT:
			ground->move(D3DXVECTOR3((float)(-.0001 * timeElapsed),0,0));
			break;
		case DIK_O:
			ground->clearPosition();
			break;
	}
}

void GroundInputComponent::Serialize(EnginePointer<ISerializerWriteContext>& context, char* name)
{
	context->StartItem(name, "GroundInputComponent", "1.0");
	context->SerializeProperty("componentId", this->componentId);
	context->SerializeProperty("groundId", this->groundId);
	context->EndItem(name);
}

IEngineComponent* GroundInputComponent::Deserialize(EnginePointer<ISerializerReadContext>& context)
{
	GroundInputComponent* input = new GroundInputComponent();
	int index = 0;
	char* childName = context->ChildName(index);

	while (childName != NULL)
	{
		if (strcmp(childName, "componentId") == 0)
		{
			input->componentId = context->ChildValueAsULongLong(index);
		}
		else if (strcmp(childName, "groundId") == 0)
		{
			input->groundId = context->ChildValueAsULongLong(index);
		}
		childName = context->ChildName(++index);
	}
	
	return input;
}

void GroundInputComponent::Initialize(EnginePointer<IEngineComponentCollection> components)
{
	ground = (EnginePointer<RenderComponent>)components->getComponentFromId(groundId);
}